package ynu.lcy.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import ynu.lcy.dto.food.AddFoodRequestDTO;
import ynu.lcy.elememicroservicecommon.annotation.AuthCheck;
import ynu.lcy.elememicroservicecommon.constant.UserConstant;
import ynu.lcy.elememicroservicecommon.utils.BaseResponse;
import ynu.lcy.elememicroservicecommon.utils.ResultUtils;
import ynu.lcy.entity.Food;
import ynu.lcy.service.FoodService;
import ynu.lcy.vo.FoodVO;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/food")
@Api(tags="商品接口")
public class FoodController {
    @Resource
    private FoodService service;

    @Resource
    private ModelMapper modelMapper;

    /**
     * 根据商家id获取商品列表
     * @param id id
     * */
    @GetMapping("/getByBusinessId/{id}")
    @ApiOperation("根据商家id获取商品列表")
    public BaseResponse<List<FoodVO>> list(@PathVariable("id") Long id){
        List<FoodVO> vos = service.getByBusinessId(id).stream().map(food -> modelMapper.map(food, FoodVO.class)).toList();
        return ResultUtils.success(vos);
    }

    /**
     * 获取商品列表
     * */
    @GetMapping("/list")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation("获取商品列表")
    public BaseResponse<List<FoodVO>> list(){
        List<FoodVO> vos = service.getAll()
                .stream()
                .map(food -> modelMapper.map(food, FoodVO.class))
                .toList();
        return ResultUtils.success(vos);
    }

    /**
     * 添加商品
     * @param dto dto
     * */
    @PostMapping("/admin/add")
    @ApiOperation("添加商品")
    @Transactional
    public BaseResponse<FoodVO> add(@ApiParam("请求实体") AddFoodRequestDTO dto){
        Food food = service.addFood(dto);
        return ResultUtils.success(modelMapper.map(food, FoodVO.class));
    }

    /**
     * 删除商品
     * @param id id
     * */
    @GetMapping("/admin/delete/{id}")
    @ApiOperation("删除商品")
    public BaseResponse<Boolean> delete(@PathVariable("id") Long id){
        return ResultUtils.success(service.delete(id));
    }
}
